1.1 Introduction to MakeCode

MakeCode is an open-source programming tool launched by Microsoft for teenagers and programming beginners. It features a dual mode of graphical drag-and-drop and code editing, lowering the threshold for programming entry and adapting to multiple hardware scenarios to quickly realize ideas.

MakeCode official website: https://makecode.microbit.org/

1.2 Introduction to micro:bit

micro:bit is a microcomputer specifically designed for children's programming education and applying technological knowledge to solve problems. HUSKYLENS 2 is compatible with micro:bit, enabling the development of various interesting visual recognition projects through graphical programming.

Purchase link for micro:bit V2:click here

2. Hardware Connection & Preparation Materials

Preparation Materials

Insert the micro:bit into the compatible expansion board. Connect the expansion board to the computer using the USB-C cable. Insert the 4-Pin I2C/UART Sensor Cable into the I2C/UART interface of HUSKYLENS 2, and connect the other end to the "5V Huskylens" port on the expansion board. Since HUSKYLENS 2 has specific power requirements, use an additional USB-C cable to connect to the power port of HUSKYLENS 2, with the other end connected to a computer or compatible power supply.

Refer to the following image for the wiring diagram:

Interface Diagram

3. Load the HUSKYLENS 2 Library in MakeCode

Go to the MakeCode website and log in at https://makecode.microbit.org/, then click Import

Interface Diagram

Select Import URL

Interface Diagram

Enter https://github.com/DFRobot/pxt-DFRobot_HuskyLensV2 and click Go ahead!

Interface Diagram

Once imported successfully, the page will appear as follows.

Interface Diagram

The current latest system version is 1.1.6. Find System Settings > Device Information to check your current system version. We recommend updating to the latest version to experience new features. Update tutorial: Click here

Interface Diagram

4. Face Recognition Project Example

4.1 Recognize Faces and Output Related Data

When face recognition is enabled, faces appearing on the HuskyLens 2 screen will be detected and framed, allowing you to access key data: total detected faces, total trained faces, center-closest face, nth detected face, and specified ID face. This data includes the face’s ID, name, center coordinates (XY), width, and height.

Example program:

Interface Diagram

Connect to micro:bit and click Download to transfer the program. After successful download, click Show Data to activate data output mode. Align the HuskyLens 2 camera with a face to observe the output.

As shown, when HuskyLens 2 detects two previously trained faces named "JACK" and "TOM", the following data will display:

How to learn faces, please refer to the [Face Recognition tutorial](https://wiki.dfrobot.com/_SKU_SEN0638_Gravity_HUSKYLENS_2_AI_Camera_Vision_Sensor#4.2 Face Recognition)

Operation Result: The serial port will output: total detected faces, ID of the center-closest face, name of the first detected face, and total the learned faces.

Interface Diagram

Interface Diagram

There are 2 detected faces, so the dot matrix display shows "2".

Interface Diagram

4.2 Obtaining Facial Feature Data

The system can capture facial feature and positional data for any detected face. Key readable information includes: face ID, name, width, height, center coordinates (X/Y), left/right eye coordinates (X/Y), left/right mouth corner coordinates (X/Y), and nose coordinates (X/Y).

For instance, the sample program below retrieves facial feature data of faces positioned near the camera’s center; this functionality works even for faces not previously learned.

Interface Diagram

Operation Result: As shown, click "Download" to transfer the program to micro:bit. After successful download, click "Show Device" to activate data output. Align HuskyLens 2 with a face, and the serial port will display the face ID along with coordinates of its key facial features.

Interface Diagram

Interface Diagram

4.3 Obtaining Data of a Specific Face

When multiple faces with the same ID appear simultaneously, the program can count their occurrences and retrieve data for the nth face in the detected sequence.

Example program:

Interface Diagram

Operation Result: As shown, click "Download" to transfer the program. Align HuskyLens 2 with two faces labeled ID 1. After activation, the serial port will output data statistics for these two faces.

Interface Diagram

Interface Diagram

4.4 Face Recognition Blocks Description

block Description
Used to obtain one set of Face Recognition data under the Face Recognition function of HUSKYLENS 2.
Determine if a human face is present in the current frame.
Retrieves data of the face closest to the screen cross cursor, including face ID, custom face name, center X/Y coordinates, width, height, left/right eye X/Y coordinates, left/right mouth corner X/Y coordinates, and nose X/Y coordinates.
Get the total number of faces detected on the screen, including both unregistered and registered faces.
When multiple faces are detected, retrieves data for a specified face.
Get the total number of learned faces , regardless of whether they appear in the current frame.
Determine if a face with the specified ID exists in the frame. Unlearned IDs are uniformly set to 0, and learned IDs are sorted in the order they were learned.
Gets the total number of detected faces with a specified ID on the screen.
Obtain the number of faces with the specified ID in the current frame.
When multiple faces are detected, retrieve the relevant data of a specified face.

Updating...